<% // Copyright 2008 Carnegie Mellon University
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//  
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, 
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License. %><?xml version="1.0" encoding="UTF-8"?>
<%@page language="java" contentType="application/xml; charset=UTF-8" %><%@page pageEncoding="UTF-8" %><%@page import="java.sql.*"%><%@page import="java.util.*"%><%@page import="java.text.*"%><%@page import="edu.cmu.lti.sepia.data.session.SessionManager"%><%@page import="edu.cmu.lti.sepia.data.resource.ResourceManager"%><%@page import="edu.cmu.lti.sepia.infra.topic.model.*"%><%@page import="edu.cmu.lti.sepia.data.db.*"%><%@page import="edu.cmu.lti.sepia.util.ServletUtil"%><%@page import="edu.cmu.lti.sepia.util.Logger"%><%@page import="edu.cmu.lti.sepia.infra.topic.GoldStandardObjToXml"%><% 
response.setHeader("progma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
request.setCharacterEncoding("UTF8");

SQL sql = new SQL();
//http://localhost:8080/sepia/export/xml_exporter2.jsp?mode=td&taskNameShort=ja_sandbox
String type = (String)request.getParameter("type");
boolean selectedOnly = request.getParameter("selected")!=null;
String taskNameShort;
if ( request.getParameter("taskNameShort")!=null ) {
	taskNameShort = (String)request.getParameter("taskNameShort");
	SessionManager.storeTask(sql, taskNameShort, session);
} else {
	taskNameShort = (String)session.getAttribute("taskNameShort");
}
String postfix = "_"+(type.equals("gs")?"goldstandard":"topics");
response.setHeader("Content-Disposition","attachment; filename=\""+taskNameShort+postfix+".xml\"");

ServletUtil s = new ServletUtil( request );
String srcLang = s.readString("srcLang","");
String trgLang = s.readString("trgLang","");
String taskDescription = s.readString("taskDescription","");
int userId = s.readInt("userId", -1);

List<Topic> topics = TopicDAO.getGoldStandard( sql, taskNameShort, userId, null );
Logger.log( sql, session, "EXPORT TOPICS (XML)", "" );
sql.close();
TopicDAO.removeSimilarNuggets(topics);
TopicDAO.generateExternalIds(topics,trgLang,4);
if (selectedOnly) {
  TopicDAO.removeUnselected(topics);
}

String xml = null;
if ( type.equals("gs") ) {
  xml = GoldStandardObjToXml.toGoldStandardXml(topics,srcLang,trgLang,taskDescription);
} else {
  xml = GoldStandardObjToXml.toQuestionXml(topics,srcLang,trgLang,taskDescription);
}
out.print(xml);
%>
